package com.agassi.edu.jee.bazar.logic;

import javax.annotation.Resource;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import com.agassi.edu.jee.bazar.persistence.Item;
import com.agassi.edu.jee.bazar.persistence.Order;
import com.agassi.edu.jee.bazar.persistence.User;

/**
 * Session Bean implementation class PlaceOrder
 */
@Stateful
public class PlaceOrderBean implements PlaceOrder {
	
	@Resource(name="jms/QueueConnectionFactory")
	private ConnectionFactory connectionFactory;
	
	@Resource(name="jms/OrderBillingQueue")
	private Destination billingQueue;
	 
	
    /**
     * Default constructor. 
     */
    public PlaceOrderBean() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public void addItem(Item item) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setBidder(User bidder) {
		// TODO Auto-generated method stub
		
	}

	@Override
	@Remove
	public void confirmOrder() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void billOrder(Order order) {
		try {
			Connection connection = connectionFactory.createConnection();
			Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
			MessageProducer orderProduccer = session.createProducer(billingQueue);
			ObjectMessage message = session.createObjectMessage();
			message.setObject(order);
			orderProduccer.send(message);
			orderProduccer.close();
			session.close();
			connection.close();		
			
		} catch (JMSException jmsException) {
			// TODO Auto-generated catch block
			jmsException.printStackTrace();
		}
		
		
	}	

}
